/**
 * 
 */
package com.rio.dandan.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.google.gson.reflect.TypeToken;
import com.rio.dandan.bean.Constants;
import com.rio.dandan.bean.UserPhoto;
import com.rio.dandan.dao.UserAlbumDao;
import com.rio.dandan.pojo.UserAlbum;
import com.rio.dandan.service.UserPhotoService;
import com.rio.dandan.util.ImageUtil;
import com.rio.dandan.util.JsonUtils;
import com.rio.dandan.util.SecurityUtils;
import com.rio.dandan.util.UUIDUtils;

/**
 * 
 * UserPhotoServiceImpl.java
 * 
 * @author 宋陆
 * @version 1.0 2014年5月22日
 */
@Service
public class UserPhotoServiceImpl implements UserPhotoService {

    @Resource
    private UserAlbumDao userAlbumDao;

    /**
     * @see com.rio.dandan.service.UserPhotoService#getUserAlbum(java.lang.String)
     */
    @Override
    public UserAlbum getUserAlbum(String uid) {
        return userAlbumDao.getUserAlbum(uid);
    }

    /**
     * @see com.rio.dandan.service.UserPhotoService#findUserPhoto(java.lang.String)
     */
    @Override
    public List<UserPhoto> findUserPhoto(String uid) {
        UserAlbum userAlbum = userAlbumDao.getUserAlbum(uid);
        List<UserPhoto> list = new ArrayList<UserPhoto>();
        if (userAlbum != null && userAlbum.getPhotos() != null) {
            list = JsonUtils.fromJson(userAlbum.getPhotos(), new TypeToken<List<UserPhoto>>() {
            });
        }
        return list;
    }

    /**
     * @see com.rio.dandan.service.UserPhotoService#sortUserPhoto(java.lang.String,
     *      java.lang.String)
     */
    @Override
    public void sortUserPhoto(String ids, String uid) {
        UserAlbum userAlbum = userAlbumDao.getUserAlbum(uid);
        if (userAlbum == null) {
            return;
        }
        if (userAlbum.getPhotos() == null) {
            return;
        }
        Map<String, UserPhoto> map = new HashMap<String, UserPhoto>();
        List<UserPhoto> photos = JsonUtils.fromJson(userAlbum.getPhotos(), new TypeToken<List<UserPhoto>>() {
        });
        for (UserPhoto userPhoto : photos) {
            map.put(userPhoto.getFileName(), userPhoto);
        }
        List<UserPhoto> newPhotos = new LinkedList<UserPhoto>();
        for (String id : ids.split(",")) {
            if (map.containsKey(id)) {
                newPhotos.add(map.get(id));
            }
        }
        userAlbum.setPhotos(JsonUtils.toJson(newPhotos, new TypeToken<List<UserPhoto>>() {
        }.getType()));

        userAlbumDao.saveOrUpdateUserAlbum(userAlbum);
    }

    @Override
    public void saveMainPhoto(String uuid, Integer x, Integer y, Integer width, Integer height) {
        String orgDir = Constants.UPLOAD_TMP_DIR + UUIDUtils.uuidDir(uuid);
        String srcImageDir = orgDir + "_tmp.jpg";
        // 裁剪
        String tarImageDir = orgDir + "_cut" + ".jpg";
        ImageUtil.cut(srcImageDir, tarImageDir, x, y, width, height);
        // 缩放至 240 * 300 large
        String largeImageDir = orgDir + "_large" + ".jpg";
        ImageUtil.scale(tarImageDir, largeImageDir, 240, 300, true);
        // 缩放至 120 * 150 medium
        String mediumImageDir = orgDir + "_medium" + ".jpg";
        ImageUtil.scale(tarImageDir, mediumImageDir, 120, 150, true);
        // 缩放至 80 * 100 small
        String smallImageDir = orgDir + "_small" + ".jpg";
        ImageUtil.scale(tarImageDir, smallImageDir, 80, 100, true);
        // 缩放后移动到用户目录

        String uid = SecurityUtils.getCurrUId();
        String targDir = Constants.UPLOAD_USER_DIR + UUIDUtils.uuidDir(uid);
        File targDirFile = new File(Constants.UPLOAD_USER_DIR + targDir);
        if (!targDirFile.exists()) {
            targDirFile.mkdirs();
        }

        try {
            FileUtils.copyFile(new File(srcImageDir), new File(targDir + File.separator + uid + "_org.jpg"));
            FileUtils.copyFile(new File(largeImageDir), new File(targDir + File.separator + uid + "_large.jpg"));
            FileUtils.copyFile(new File(mediumImageDir), new File(targDir + File.separator + uid + "_medium.jpg"));
            FileUtils.copyFile(new File(smallImageDir), new File(targDir + File.separator + uid + "_small.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @see com.rio.dandan.service.UserPhotoService#saveAlbumPhoto(java.lang.String)
     */
    @Override
    public void saveAlbumPhoto(String uuid, String suffix) {
        String orgDir = Constants.UPLOAD_TMP_DIR + UUIDUtils.uuidDir(uuid);
        String srcImageDir = orgDir + "_tmp.jpg";

        // 缩放至 240 * 300 large
        String largeImageDir = orgDir + "_large" + ".jpg";
        ImageUtil.scale(srcImageDir, largeImageDir, 240, 300, false);
        // 缩放至 120 * 150 medium
        String mediumImageDir = orgDir + "_medium" + ".jpg";
        ImageUtil.scale(srcImageDir, mediumImageDir, 120, 150, false);
        // 缩放至 80 * 100 small
        String smallImageDir = orgDir + "_small" + ".jpg";
        ImageUtil.scale(srcImageDir, smallImageDir, 80, 100, false);
        // 缩放后移动到用户目录

        String uid = SecurityUtils.getCurrUId();
        String targDir = Constants.UPLOAD_USER_DIR + UUIDUtils.uuidDir(uid);
        File targDirFile = new File(Constants.UPLOAD_USER_DIR + targDir);
        if (!targDirFile.exists()) {
            targDirFile.mkdirs();
        }

        try {
            FileUtils.copyFile(new File(srcImageDir), new File(targDir + File.separator + uuid + "_org.jpg"));
            FileUtils.copyFile(new File(largeImageDir), new File(targDir + File.separator + uuid + "_large.jpg"));
            FileUtils.copyFile(new File(mediumImageDir), new File(targDir + File.separator + uuid + "_medium.jpg"));
            FileUtils.copyFile(new File(smallImageDir), new File(targDir + File.separator + uuid + "_small.jpg"));

            UserAlbum userAlbum = userAlbumDao.getUserAlbum(uid);
            if (userAlbum == null) {
                userAlbum = new UserAlbum();
                userAlbum.setUid(uid);
            }
            if (userAlbum.getPhotos() == null) {
                userAlbum.setPhotos("[]");
            }
            List<UserPhoto> photos = JsonUtils.fromJson(userAlbum.getPhotos(), new TypeToken<List<UserPhoto>>() {
            });

            UserPhoto photo = new UserPhoto();
            photo.setCreateTime(new Date());
            photo.setFileName(uuid);
            photo.setSummary(StringUtils.EMPTY);
            photo.setStatus((short) 1);
            photo.setType((short) 1);
            photos.add(photo);

            userAlbum.setPhotos(JsonUtils.toJson(photos, new TypeToken<List<UserPhoto>>() {
            }.getType()));

            userAlbumDao.saveOrUpdateUserAlbum(userAlbum);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
